// 本地缓存温度对象的Key
const temperatureObjectKey = "temperatureObject";

// 更新本地缓存的温度值
function getLocalTemperature(temperatureKey) {
    // 获取本地缓存的温度数据
    let temperatureObject = serialLocalStorage.get(temperatureObjectKey)

    // 返回指温度的值
    let temperature = temperatureObject[temperatureKey];

    console.log("查询的温度类型[" + temperatureKey + "]的值：" + temperature + "成功");
    return temperature;
}

// 更新本地缓存的温度值
function updateLocalTemperature(temperatureKey, temperatureValue) {
    // 获取本地缓存的温度数据
    let temperatureObject = serialLocalStorage.get(temperatureObjectKey)

    // 更新指定类型的温度
    temperatureObject[temperatureKey] = temperatureValue

    // 更新本地存储的开关数据
    serialLocalStorage.put(temperatureObjectKey, temperatureObject)

    console.log("更新设置温度类型[" + temperatureKey + "]的值：" + temperatureValue + "成功");
    return true;
}


/**
 * setFrontCabinTemperature 设置前舱温度
 * @param temperature
 * @param registerAddress
 */
function setFrontCabinTemperature(temperature, registerAddress = 0x0013) {
    // 获取温度开关状态
    let temperatureSwitchObject = serialLocalStorage.get("settingSwitchObject");
    // 判断开关是否开启
    if (temperatureSwitchObject == null || !temperatureSwitchObject.front_cabin_temperature_switch) {
        plus.nativeUI.alert("前艙溫度開關未打開！")
        return false;
    }

    // 发送命令
    setRegisterValueReq(registerAddress, temperature);

    // 更新本地缓存温度数据
    updateLocalTemperature("front_cabin_temperature", temperature)
    return true;
}


/**
 * setRearCabinTemperature 设置后舱温度
 * @param temperature
 * @param registerAddress
 */
function setRearCabinTemperature(temperature, registerAddress = 0x0014) {
    // 获取温度开关状态
    let temperatureSwitchObject = serialLocalStorage.get("settingSwitchObject")
    // 判断开关是否开启
    if (temperatureSwitchObject == null || !temperatureSwitchObject.rear_cabin_temperature_switch) {
        plus.nativeUI.alert("后艙溫度開關未打開！")
        return false;
    }

    // 发送命令
    setRegisterValueReq(registerAddress, temperature);

    // 更新本地缓存温度数据
    updateLocalTemperature("rear_cabin_temperature", temperature)
    return true;
}


/**
 * setRockTemperature 设置岩盘温度
 * @param temperature
 * @param registerAddress
 */
function setRockTemperature(temperature, registerAddress = 0x0015) {
    // 获取温度开关状态
    let temperatureSwitchObject = serialLocalStorage.get("settingSwitchObject")

    // 判断开关是否开启
    if (temperatureSwitchObject == null || !temperatureSwitchObject.rock_temperature_switch) {
        plus.nativeUI.alert("岩盤溫度開關未打開！")
        return false;
    }

    // 发送命令
    setRegisterValueReq(registerAddress, temperature);

    // 更新本地缓存温度数据
    updateLocalTemperature("rock_temperature", temperature)
    return true;
}
